package controller;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;

import bean.Right;
import bean.User;
import cn.dsna.util.images.ValidateCode;
import service.UserService;
import util.MD5Util;
import util.RSAUtil;

@Controller
public class LoginController {
	private UserService userservice = new UserService();

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ResponseEntity<JSONObject> login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String password = request.getParameter("password");
		String username = request.getParameter("username");
		String checkcode = request.getParameter("checkcode");
		System.out.println("用户名:" + username);
		System.out.println("加密后的密码：" + password);
		System.out.println("验证码:" + checkcode);

		String code = (String) request.getSession().getAttribute("code");
		String privateKey = (String) request.getSession().getAttribute("privateKey");
		String descrypedPwd = new String(RSAUtil.decryptByPrivateKey(password, privateKey));

		System.out.println("解密后的密码：" + descrypedPwd);
		String md5password = MD5Util.MD5(descrypedPwd);

		User user = userservice.login(username);
		boolean result = false;
		if (user.getUserId() != 0) {
			if (code.equals(checkcode) && md5password.equals(user.getUserPassword())) {
				request.getSession().setAttribute("user", user);
				result = true;
				List<Right> userMenu = userservice.getUserMenu(user);
				List<Right> type1 = new ArrayList<>();
				List<Right> type2 = new ArrayList<>();
				List<Right> type3 = new ArrayList<>();
				List<Right> type4 = new ArrayList<>();
				for (Right right : userMenu) {
					switch (right.getType()) {
					case 1:
						type1.add(right);
						break;
					case 2:
						type2.add(right);
						break;
					case 3:
						type3.add(right);
						break;
					case 4:
						type4.add(right);
						break;
					default:
						break;
					}
				}
				request.getSession().setAttribute("type1", type1);
				request.getSession().setAttribute("type2", type2);
				request.getSession().setAttribute("type3", type3);
				request.getSession().setAttribute("type4", type4);
			}
		}
		JSONObject json = new JSONObject();
		json.put("result", result);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json, responseHeaders,
				HttpStatus.OK);
		return responseResult;
	}

	/**
	 * 生成验证码
	 */
	@RequestMapping(value = "/code", method = RequestMethod.GET)
	public void genarateCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ValidateCode validateCode = new ValidateCode(180, 40, 4, 80);
		String code = validateCode.getCode();// 得到验证码的具体字符
		request.getSession().setAttribute("code", code);// 将验证码字符存到session中

		response.setContentType("image/jpeg"); // 设置响应类型为图片
		System.out.println(code);
		validateCode.write(response.getOutputStream());// 将生成的验证码写到页面中
	}

	@RequestMapping("/toRegister")
	public String toRegister() {
		return "register";
	}
}
